package system

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzsystemsmsrecord"
	"private-domain-overseas-service/tools/ent/predicate"
)

type SystemSmsRecordDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemSmsRecordDao(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSmsRecordDao {
	return &SystemSmsRecordDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// SystemSmsRecordCreate - 新建短信
func (l *SystemSmsRecordDao) SystemSmsRecordCreate(sms *ent.HzSystemSmsRecord, tx *ent.Tx) (*ent.HzSystemSmsRecord, error) {
	clientDao := l.svcCtx.Orm.HzSystemSmsRecord
	if tx != nil {
		clientDao = tx.HzSystemSmsRecord
	}
	res, err := clientDao.Create().
		SetSystemSmsArea(sms.SystemSmsArea).
		SetSystemSmsPhone(sms.SystemSmsPhone).
		SetSystemSmsCaptcha(sms.SystemSmsCaptcha).
		SetSystemSmsMessage(sms.SystemSmsMessage).
		SetSystemSmsDescription(sms.SystemSmsDescription).
		SetSystemSmsType(sms.SystemSmsType).
		SetSystemSmsPlatform(sms.SystemSmsPlatform).
		SetSystemSmsStatus(sms.SystemSmsStatus).
		SetSystemSmsSendAt(sms.SystemSmsSendAt).
		SetSystemSmsExpireAt(sms.SystemSmsExpireAt).
		SetCreatedAt(sms.CreatedAt).
		SetUpdateAt(sms.UpdateAt).
		Save(l.ctx)
	if err != nil {
		return res, err
	}
	return res, nil
}

// SystemSmsRecordUpdate - 编辑验证码
func (l *SystemSmsRecordDao) SystemSmsRecordUpdate(sms *ent.HzSystemSmsRecord, tx *ent.Tx) error {
	clientDao := l.svcCtx.Orm.HzSystemSmsRecord
	if tx != nil {
		clientDao = tx.HzSystemSmsRecord
	}
	err := clientDao.UpdateOneID(sms.ID).
		SetSystemSmsArea(sms.SystemSmsArea).
		SetSystemSmsPhone(sms.SystemSmsPhone).
		SetSystemSmsCaptcha(sms.SystemSmsCaptcha).
		SetSystemSmsMessage(sms.SystemSmsMessage).
		SetSystemSmsDescription(sms.SystemSmsDescription).
		SetSystemSmsType(sms.SystemSmsType).
		SetSystemSmsPlatform(sms.SystemSmsPlatform).
		SetSystemSmsStatus(sms.SystemSmsStatus).
		SetSystemSmsSendAt(sms.SystemSmsSendAt).
		SetSystemSmsExpireAt(sms.SystemSmsExpireAt).
		SetUpdateAt(sms.UpdateAt).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// SystemSmsRecordUpdateStatusByConditions - 根据条件更新验证码使用状态
func (l *SystemSmsRecordDao) SystemSmsRecordUpdateStatusByConditions(condition []predicate.HzSystemSmsRecord, sms *ent.HzSystemSmsRecord, tx *ent.Tx) error {
	clientDao := l.svcCtx.Orm.HzSystemSmsRecord
	if tx != nil {
		clientDao = tx.HzSystemSmsRecord
	}

	// 查找指定手机号的所有记录并更新状态
	err := clientDao.Update().
		Where(condition...).
		SetSystemSmsStatus(sms.SystemSmsStatus).
		SetUpdateAt(sms.UpdateAt).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// SystemSmsRecordConditionsFirst - 根据条件查询单条验证码信息
func (l *SystemSmsRecordDao) SystemSmsRecordConditionsFirst(conditions []predicate.HzSystemSmsRecord) (*ent.HzSystemSmsRecord, error) {
	verify, err := l.svcCtx.Orm.HzSystemSmsRecord.Query().
		Where(conditions...).
		Order(ent.Desc(hzsystemsmsrecord.FieldID)).
		First(l.ctx)
	if err != nil {
		return nil, err
	}
	return verify, nil
}

// SystemSmsRecordConditionsAllByPage - 分页查询验证码信息
func (l *SystemSmsRecordDao) SystemSmsRecordConditionsAllByPage(conditions []predicate.HzSystemSmsRecord, current, pageSize int64) ([]*ent.HzSystemSmsRecord, int64, error) {
	query := l.svcCtx.Orm.HzSystemSmsRecord.Query().Where(conditions...)

	total, err := query.Count(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	records, err := query.
		Offset(int((current - 1) * pageSize)).
		Order(ent.Desc(hzsystemsmsrecord.FieldID)).
		Limit(int(pageSize)).
		All(l.ctx)
	if err != nil {
		return nil, 0, err
	}

	return records, int64(total), nil

}

// SystemSmsRecordConditionsCount - 根据条件统计验证码的数量
func (l *SystemSmsRecordDao) SystemSmsRecordConditionsCount(conditions []predicate.HzSystemSmsRecord) (int, error) {
	count, err := l.svcCtx.Orm.HzSystemSmsRecord.
		Query().
		Where(conditions...).
		Count(l.ctx)
	if err != nil {
		return 0, err
	}
	return count, nil
}
